package com.tbs.parkingsaleapp.UI.Listenners;

import android.view.View;
import android.widget.Toast;
import com.tbs.parkingsaleapp.Http.AsycHttp;
import com.tbs.parkingsaleapp.Http.HttpHelper;
import com.tbs.parkingsaleapp.Http.IAsycHttpResultAction;
import com.tbs.parkingsaleapp.Http.Listenner.RegHttper;
import com.tbs.parkingsaleapp.R;
import com.tbs.parkingsaleapp.SystemManager.NetWorkSetting;
import com.tbs.parkingsaleapp.UI.LoginOrSign.Base.SignFragment;

public class SignClickListenner implements View.OnClickListener, IAsycHttpResultAction<Integer> {

    SignFragment f;

    public SignClickListenner(SignFragment fg) {
        f = fg;
    }

    View v;

    @Override
    public void onClick(View view) {
        SignFragment.UserData data = f.Collect();
        if (data.phone.length() == 0) {
            Toast.makeText(f.getContext(), "手机号未填写", Toast.LENGTH_SHORT).show();
            return;
        }
        if (data.id.length() == 0) {
            Toast.makeText(f.getContext(), "身份证未填写", Toast.LENGTH_SHORT).show();
            return;
        }
        if (data.name.length() == 0) {
            Toast.makeText(f.getContext(), "用户名未填写", Toast.LENGTH_SHORT).show();
            return;
        }
        if (data.password.length() == 0) {
            Toast.makeText(f.getContext(), "密码未填写", Toast.LENGTH_SHORT).show();
            return;
        }
        if (f.isFaceisok()) {
            v = view;
            view.setEnabled(false);

            AsycHttp<SignFragment.UserData, Integer> reg = new AsycHttp<>();
            reg.setBackgroundAction(new RegHttper()).setContext(f.getContext());
            reg.setResultAction(this);
            reg.execute(data);
        } else {
            Toast.makeText(f.getContext(), "您的人脸尚未通过，仅支持匿名使用", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void ResultFunction(Integer result) {
        v.setEnabled(true);
        String text = "";
        switch (result) {
            case -1:
                text = "用户名或密码长度不足";
                break;
            case -2:
                text = "用户名已存在";
                break;
            case -3:
                text = "您已注册";
                break;
            case -4:
                text = "我们这边出错了";
                break;
            case 1:
                text = "注册成功";
                break;
            case 0:
                text = "人脸无效";
                break;
        }
        Toast.makeText(f.getContext(), text, Toast.LENGTH_SHORT).show();
    }
}
